using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using MyBehaviorTree;

public class CheckEnemyInAttackRange : Node
{
    private static int _enemyLayerMask = 1 << 6;

    private Transform _transform;
    private Animator _animator;

    public CheckEnemyInAttackRange(Transform transform)
    {
        _transform = transform;
        _animator = transform.GetChild(0).GetComponent<Animator>();
    }

    public override NodeState Evaluate()
    {
        object t = GetData("target");
        if (t == null)
        {
            _PlayIdle();
            state = NodeState.FAILURE;
            return state;
        }

        Transform target = (Transform)t;
        if (Vector3.Distance(_transform.position, target.position) <= GuardBT.attackRange)
        {
            _PlayAttack();
            state = NodeState.SUCCESS;
            return state;
        }

        _PlayIdle();
        state = NodeState.FAILURE;
        return state;
    }

    private void _PlayAttack()
    {
        _animator.SetBool("Idle", false);
        _animator.SetBool("Attack", true);
    }

    private void _PlayIdle()
    {
        _animator.SetBool("Idle", true);
        _animator.SetBool("Attack", false);
    }
}
